﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;

namespace WebApplication1
{
    public partial class updateFund : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FundAL fundAL = new FundAL();
            List<FundDTO> fundDTO = fundAL.GetAllFund();
            gvUpdateFund.DataSource = fundDTO;
            gvUpdateFund.DataBind();
           
        }

        private void BindGrid()
        {
            
            try
            {
                FundAL fundAL = new FundAL();
                List<FundDTO> fundDTO = fundAL.GetAllFund();
                gvUpdateFund.DataSource = fundDTO;
                gvUpdateFund.DataBind();
            }
            catch(Exception ee)
            {
                lblUpdateFund.Text = "There are some exceptions occuured while binding the data with Grid" + ee.Message.ToString();
            }
        }

      

        protected void CancelRecord(object sender, GridViewCancelEditEventArgs e)
        {

        }

        protected void gvUpdateFund_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Guid fundId = new Guid(gvUpdateFund.DataKeys[e.RowIndex].Value.ToString());
            try
            {
                FundAL fundAL = new FundAL();
                fundAL.deleteFundById(fundId);
            }
            catch (Exception ee)
            {
                lblUpdateFund.Text = "There is an exception occured while deleting the fund detail " + ee.Message.ToString();
            }
            gvUpdateFund.EditIndex = -1;
            BindGrid();
        }

        protected void gvUpdateFund_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Guid fundId = new Guid(gvUpdateFund.DataKeys[e.RowIndex].Value.ToString());
            GridViewRow row = gvUpdateFund.Rows[e.RowIndex];
            TextBox tTitle = (TextBox)row.FindControl("txtUpdFundTitle");
            TextBox tDescription = (TextBox)row.FindControl("txtUpdFundDescription");
            TextBox tDonatedBy = (TextBox)row.FindControl("txtUpdFundDonatedBy");
            TextBox tPurpose = (TextBox)row.FindControl("txtUpdFundPurpose");
            TextBox tValue = (TextBox)row.FindControl("txtUpdFundValue");

            FundAL fundAL = new FundAL();
            try
            {
                FundDTO fundDTO = new FundDTO
                {
                    FundId = fundId,
                    Title = tTitle.Text,
                    Description = tDescription.Text,
                    Purpose = tPurpose.Text,
                    DonatedBy = tDonatedBy.Text,
                    Value = Convert.ToDecimal(tValue.Text),
                    // ModifiedBy = "Tharsan",
                    ModifiedTime = System.DateTime.Now
                };
                string result = fundAL.saveFund(fundDTO);
                if (result == "Saved")
                {
                    lblUpdateFund.Text = "Fund Details added Successfully";

                }
                else if (result == "Updated")
                {
                    lblUpdateFund.Text = "Fund Details updated Successfully";

                }
                else if (result == "AlreadyExists")
                {
                    lblUpdateFund.Text = "The Fund name added by you is already exists";
                }
                else
                {
                    lblUpdateFund.Text = "Your data cannot be added";
                }
            }
            catch (Exception ee)
            {
                lblUpdateFund.Text = "There is an exception occured while inserting the fund detail" + ee.Message.ToString();
            }
        }

        protected void gvUpdateFund_RowEditing(object sender, GridViewEditEventArgs e)
        {
            Guid fundId = new Guid(gvUpdateFund.DataKeys[e.NewEditIndex].Value.ToString());
            GridViewRow row = gvUpdateFund.Rows[e.NewEditIndex];
            TextBox tTitle = (TextBox)row.FindControl("txtUpdFundTitle");
            TextBox tDescription = (TextBox)row.FindControl("txtUpdFundDescription");
            TextBox tDonatedBy = (TextBox)row.FindControl("txtUpdFundDonatedBy");
            TextBox tPurpose = (TextBox)row.FindControl("txtUpdFundPurpose");
            TextBox tValue = (TextBox)row.FindControl("txtUpdFundValue");

            FundAL fundAL = new FundAL();
            try
            {
                FundDTO fundDTO = new FundDTO
                {
                    FundId = fundId,
                    Title = tTitle.Text,
                    Description = tDescription.Text,
                    Purpose = tPurpose.Text,
                    DonatedBy = tDonatedBy.Text,
                    Value = Convert.ToDecimal(tValue.Text),
                    // ModifiedBy = "Tharsan",
                    ModifiedTime = System.DateTime.Now
                };
                string result = fundAL.saveFund(fundDTO);
                if (result == "Saved")
                {
                    lblUpdateFund.Text = "Fund Details added Successfully";

                }
                else if (result == "Updated")
                {
                    lblUpdateFund.Text = "Fund Details updated Successfully";

                }
                else if (result == "AlreadyExists")
                {
                    lblUpdateFund.Text = "The Fund name added by you is already exists";
                }
                else
                {
                    lblUpdateFund.Text = "Your data cannot be added";
                }
            }
            catch (Exception ee)
            {
                lblUpdateFund.Text = "There is an exception occured while inserting the fund detail" + ee.Message.ToString();
            }
        }
    }
}